<template>
  <div class="xtx-form">
    <div class="user-info">
      <img v-if="avatar" :src="avatar" alt="" />
      <img v-else src="@/assets/images/loading.gif" alt="" />
      <p>Hi，{{ nickname }} 欢迎来小兔鲜，完成绑定后可以QQ账号一键登录哦~</p>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input
          class="input"
          type="text"
          placeholder="绑定的手机号"
          v-model="form.mobile"
        />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input
          class="input"
          type="text"
          placeholder="短信验证码"
          v-model="form.code"
        />
        <span class="code" @click="sendCode">
          <span>{{
            countTime === 0 ? "发送验证码" : `${countTime}秒后尝试`
          }}</span>
        </span>
      </div>
      <div class="error"></div>
    </div>
    <a href="javascript:;" class="submit" @click="submit()">立即绑定</a>
  </div>
</template>

<script>
import { reactive, ref } from 'vue-demi'
import QC from 'qc'
import { useInterval } from '@/compositions/interval'
import { reqUserQQBindCode, reqUserQQBindLogin } from '@/api/user'
import { message } from '@/components/Message'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
// import { reqUserQQLogin } from '@/api/user'
export default {
  name: 'CallbackBind',
  props: {
    unionId: {
      typr: String,
      default: ''
    }
  },
  setup (props) {
    // 头像
    const avatar = ref('')
    // 昵称
    const nickname = ref('')
    const form = reactive({
      mobile: '',
      code: ''
    })
    // 调用QQ互联的函数来获取登录QQ的昵称和头像
    if (QC.Login.check()) {
      QC.api('get_user_info').success((res) => {
        // //console.log('QQ互联的函数', res)
        avatar.value = res.data.figureurl_qq_2
        nickname.value = res.data.nickname
      })
    }
    // 倒计时函数
    const { start, countTime } = useInterval(60)
    // 获取验证码
    const sendCode = async () => {
      // 用正则表达式校验手机号码
      if (!/^1[3-9]\d{9}$/.test(form.mobile)) { return message({ text: '手机号码格式错误' }) }
      // const { result } = await reqUserQQPatchCode(form.mobile)
      // //console.log('获取回来的手机校验数据', result)
      // 发送请求获取验证码
      await reqUserQQBindCode(form.mobile)
      // 调用倒计时计时的开始函数
      start()
    }
    const store = useStore()
    const router = useRouter()
    // 点击绑定提交
    const submit = async () => {
      const { result } = await reqUserQQBindLogin({
        unionId: props.unionId,
        ...form
      })
      // //console.log(result)
      // 存到store仓库中
      store.commit('user/setProfile', result)
      //  跳转页面
      router.push('/')
      // 提示用户
      message({ type: 'success', text: 'QQ绑定成功' })
    }
    return { form, avatar, nickname, sendCode, countTime, submit }
  }
}
</script>

<style scoped lang='less'>
.user-info {
  width: 320px;
  height: 70px;
  margin: 0 auto;
  display: flex;
  background: #f2f2f2;
  align-items: center;
  padding: 0 10px;
  margin-bottom: 25px;
  img {
    background: #f2f2f2;
    width: 50px;
    height: 50px;
  }
  p {
    padding-left: 10px;
  }
}
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
